<?

function today()
{
   return date('Y.m.d');
}

//------------------------------------------------------------------------------

function now()
{
   return date('Y.m.d H:i:s');
}

//------------------------------------------------------------------------------

function datef($date)
{
   return strftime(_date_format, strtotime($date));
}


//------------------------------------------------------------------------------
//------------------------------------------------------------------------------


function opts_selected($count, $id)
{
   $opts = array_fill(1, $count, ' ');
   $opts[$id] = 'selected';
   
   return $opts;
}

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------


function utfwordwrap($str, $len=50, $break=' ', $cut=false)
{
    if(empty($str)) return '';

    $pattern = '';
    
    if(!$cut)
            $pattern = "/(\S{".$len."})/u";
    else
            $pattern = "/(.{".$len."})/u";

    return preg_replace($pattern, "\${1}".$break, $str);
}

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------


function justwords($str)
{
   $str = str_replace( array('<br>', '<br/>', '<br />'), ' ', $str);
   $str = strip_tags($str);
   
   $xa = array('&nbsp;', '&',    '"',       "'",    '<',    '>',    '\\',    '/'    ); //"'
   $xb = array(' ', '&amp;', '&quot;',   '&#39;',  '&lt;', '&gt;',  '&#92;', '&#47;'); // ^oops
   
   $str = str_replace($xa, $xb, $str);
   
   $str = utfwordwrap($str, 80, ' ');
   
   while( strstr($str, '  ') )
   {
      $str = str_replace('  ', ' ', $str);
   }

   $str = preg_replace("/[^\sa-zа-яё0-9\!\?\.\,\;\:\_\*\@\+\-\%\=\&\#\(\)\[\]\{\}\n«»]/iu", '', $str);
   
   return $str;
}

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------

function justhtml($str)
{
   $str = str_replace("&nbsp;", ' ', $str);

   $xr = array('<br/>', '<br />', "\r\n", "\n\r", "\r", '</p><p>', "</p>\n<p>", '</div><div>', "</div>\n<div>", '</span><span>',"</span>\n<span>");
   $str = str_replace($xr,"<br>", $str);

   $str = str_replace("<br>\n<br>", "<br>", $str);

   $str = strip_tags($str, '<pre><a><b><strong><i><u><br><em>');

   $str = utfwordwrap($str, 300, ' ');

   while( strstr($str, '  ') )
   {
      $str = str_replace('  ', ' ', $str);
   }

   $str = preg_replace("/[^\sa-zа-яё0-9\!\?\.\,\;\:\_\*\+\-\=\%\&\#\(\)\[\]\{\}\n\>\<\/\\\'\"«»]/iu", '', $str);

   return $str;
}

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------


function justdigs($str)
{
   $str = preg_replace('/[^0-9]/', '', $str);
   
   return $str;
}

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------


function readf($fname)
{
   $fname = realpath($fname);
   
   $workdir = dirname($_SERVER['SCRIPT_FILENAME']);
   $filedir = dirname($fname);
   $docroot = $_SERVER['DOCUMENT_ROOT'];

   if($filedir == $docroot) return "<!-- stop! i don't wanna read files from this dir. -->\n";
   
   if( !strstr($filedir, $docroot) ) return "<!-- no-no! i don't wanna read file, which placed out of docroot! -->\n";

   if( !is_file($fname) ) return '<!-- stop! what do you wanna read? -->\n';


   $f = fopen($fname, 'r');
   flock($f, LOCK_SH);
   $s = fread($f, filesize($fname));
   flock($f, LOCK_UN);
   fclose($f);

   $badwords = array('<?', '?>');

   $s = str_replace($badwords, '', $s);

   return $s;
}

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------


function fmtbytes($bytes, $precision = 2)
{
    $units = array('bytes', 'kb', 'mb', 'gb', 'tb');

    $bytes  = max($bytes, 0);
    
    $pow = floor( ($bytes ? log($bytes) : 0) / log(1024) );
    $pow = min($pow, (count($units) - 1) );

    $bytes /= pow(1024, $pow);

    return round($bytes, $precision) . ' ' . $units[$pow];
}

//------------------------------------------------------------------------------


function listdir($dir)
{
   if( is_dir($dir) )
   {
      $d = opendir($dir);
      
      $files = array();
      
      while( $f = readdir($d) )
      {
         if( is_file($dir.'/'.$f) ) $files[] = array('name' => justwords($f), 'size' => fmtBytes(filesize($dir.'/'.$f), 2) );
      }
      
      if(count($files)>0) return $files;
      else return false;
   }
   else return false;
}

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------

$_x_counter = 0; // for call-back-counting of various things

function needpager($all, $onpage)
{
   return ($all > $onpage) ? ceil($all / $onpage) : 0;
}

function html_pager($pages, $currentpage)
{
   if($pages<=0) return '';
   
   $pager = '';
   
   $e_tpl = get_template('pager_elem.html');
   $c_tpl = get_template('pager_celem.html');
   $e_kws = array('_NUM_');
   
   $d = get_template('pager_delim.html');

   for($i=0; $i<$pages; $i++)
   {
      $pager .= ($i != $currentpage) ? str_replace($e_kws, ($i+1), $e_tpl) . ( ($i+1)<$pages ? $d : '' )
                                     : str_replace($e_kws, ($i+1), $c_tpl) . ( ($i+1)<$pages ? $d : '' );
   }
   
   $tpl = get_template('pager.html');
   $kws = array('_PAGES_');

   return str_replace($kws, $pager, $tpl);
}


//------------------------------------------------------------------------------
//------------------------------------------------------------------------------

function go_back()
{
   die('<html><header><script>history.back();</script></header></html>');
}


//------------------------------------------------------------------------------
//------------------------------------------------------------------------------


function sendemail($user, $message)
{
   if( !is_array($user) || !isset($user['email']) || !is_array($message)) return false;
   
   $to      = $user['email'];
   $subject = '=?UTF-8?B?'. base64_encode( justwords($message['subject']) ) .'?=';
   $message = justhtml($message['text']);
   $headers =  'From: '. _systememail. "\r\n".
               'Content-Type: text/plain; charset=UTF-8;'. "\r\n".
               'Reply-To: '. _systememail. "\r\n".
               'X-Mailer: '. _version;

   if( strlen($subject)>1 && strlen($message)>1 && mail($to, $subject, $message, $headers) ) return true;
   else return false;
}

function message2admin($message)
{
   $admin = array('email' => _adminemail);
   
   return sendemail($admin, $message);
}

function message2users($users, $message, $title)
{
	if( !is_array($users) || strlen($message)<_comment_length || strlen($title)<_title_length) return false;
	
	$m = array('text' => $message, 'subject' => $title);
	
	$x = 0;
	
	if( isset($users['email']) )
	{
		sendemail($users, $m);
		
		return 1;
	}
	elseif( is_array( current($users) ) )
	{
		global $current_user;
		
		foreach ($users as $user)
		{
			if( isset($user['email']) && (!isset($user['id']) || $user['id']!=$current_user['id']) )
			{
				sendemail($user, $m);
				$x++;
			}
		}
		
		return $x;
	}
}



























?>
